<!DOCTYPE>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>数组解构</title>

    <script src="js/traceur.js"></script>
    <script src="js/bootstrap.js"></script>

    <script type="module">
        var myArray = [1, 2, 3];
        var a = myArray[0];
        var b = myArray[1];
        var c = myArray[2];

        let myArray1 = [1, 2, 3];
        let a1, b1, c1;
        [a1, b1, c1] = myArray1; //数组解构赋值语法

        let [a2, , b2] = [1, 2, 3];
        console.log(a2, b2); //1, 3

        let [a3, ...b3] = [1, 2, 3, 4, 5];
        console.log(a3); // 1
        console.log(Array.isArray(b3)); //true
        console.log(b3); //2, 3, 4, 5

        let [a4, , , ...b4] = [1, 2, 3, 4, 5, 6, 7];
        console.log(a4); // 1
        console.log(b4); // 4, 5, 6, 7

        let [a5, b5, c5 = 3] = [1, 2];
        console.log(c5); // 3

        let [a6, b6, [c6, d6]] = [1, 2, [3, 4]];
        console.log(c6); // 3
    </script>

    <!--用解构赋值作为参数-->
    <script type="module">
        function myFunction([a, b, c=3]) {
            console.log(a, b, c); // 1, 2, 3
        }
        myFunction([1, 2]);

        function myFunction1([a, b, c = 3] = [1, 2, 3] ) {
            console.log(a, b, c); //1, 2, 3
        }
        myFunction1(undefined);
    </script>

    <!--对象-->
    <script type="module">
        var object = {"name": "john", "age": "23"};
        var name = object.name;
        var age = object.age;

        let object1 = {"name": "john", "age": "23"};
        let name1, age1;
        ({name1, age1} = object1); //对象解构赋值语法

        //变量名必须与对象属性相同
        let {name2: x, age2: y} = {"name2": "john", "age2": "23"};

        //给变量提供默认值
        let {a7, b7, c7=3} = {"a7": "1", "b7": "2"};
        console.log(c7); // 3

        //给属性的变量提供一个名称
        let {["first"+"Name"]: x1} = { firstName: "Eden" };
        console.log(x1); //"Eden"

        let{name3, otherInfo: {age3}} = {"name3": "Eden", "otherInfo": {age3: 23}};
        console.log(name3, age3); //Eden 23


        function myFunction3({name4 = 'Eden', age4 = 23, profession ="Designer"} = {})
        {
            console.log(name4, age4, profession); //"John 23 Designer"
        }
        myFunction3({name4: "John", age4: 23});
    </script>
</head>
</html>